<?php
namespace app\admin\model;
use think\Model;
use think\Db;

/**
 * 后台渠道key模型
 * @package app\admin\model
 */
class Channelkey extends Model
{
    // 设置当前模型对应的完整数据表名称
    protected $table = 'bus_channel_key';
    protected $observerClass = 'app\admin\event\DeleteCache';

    public function addKey($channel_id)
    {
        $data = [
            'cid' => $channel_id,
            'cid_key' => strtoupper(md5(uniqid()))
            ];
        return $this::isUpdate(false)->save($data);
    }

    //获取当前key
    public function getKeyArr($channel_id)
    {
        // $data['cid'] = $channel_id;
        return $this->where('cid',$channel_id)->find();
    }

    //检测当前渠道id是否存在
    public function findKey($channel_id)
    {
        return $this->where('cid',$channel_id)->find();
        // $data['cid'] = $channel_id;
        // return $this::get($data);
    }

    //检测并更新key
    public function checkKey($channel_id,$k)
    {
        return $this::isUpdate(true)->allowField(true)->save(['cid_key'=>$k],['cid'=>$channel_id]);
    }

    //重置key
    public function reset()
    {
        return strtoupper(md5(uniqid()));
    }
}
